﻿using Materal.Extensions;
using System.Reflection;

namespace WinFormTest
{
    public partial class MainForm : Form
    {
        public MainForm() => InitializeComponent();

        private void MainForm_Load(object sender, EventArgs e)
        {
            IEnumerable<Type> pageTypes = GetType().Assembly.GetTypes().Where(m => m.IsAssignableTo<UserControl>());
            foreach (Type pageType in pageTypes)
            {
                MenuAttribute? menuAttribute = pageType.GetCustomAttribute<MenuAttribute>();
                if (menuAttribute is null) return;
                Button button = new()
                {
                    Text = menuAttribute.Name,
                    Dock = DockStyle.Top,
                    Height = 34,
                    Tag = pageType
                };
                button.Click += Button_Click;
                menuPanel.Controls.Add(button);
            }
        }

        private void Button_Click(object? sender, EventArgs e)
        {
            mainPanel.Controls.Clear();
            if (sender is not Button button || button.Tag is not Type controlType) return;
            UserControl control = controlType.Instantiation<UserControl>();
            control.Dock = DockStyle.Fill;
            mainPanel.Controls.Add(control);
        }
    }
}
